package com.jingdong.controller;

import com.jingdong.model.Product;
import com.jingdong.model.Shopping_cart;
import com.jingdong.service.product.ProductService;
import com.jingdong.service.shoppingCart.ShoppingCartService;
import com.jingdong.vo.ShoppingCartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private ProductService productService;

    @RequestMapping("save")
    public ModelAndView save(Integer id, Integer count, Integer productId, Integer uID, Integer status) {
        Shopping_cart cart = new Shopping_cart();
        cart.setProduct_count(count.longValue());
        cart.setProduct_id(productId.longValue());
        cart.setUid(uID.longValue());
        cart.setProduct_status(status.longValue());
        int saveResult = shoppingCartService.save(cart);
        ModelAndView mav = new ModelAndView("shoppingCart/addCartSuccess");
        return mav;
    }

    @RequestMapping("list")
    public ModelAndView list(Long uid) {
        Long productStatus = 0L;
        Map<String, Long> map = new HashMap<String, Long>();
        map.put("uid", uid);
        map.put("productStatus", productStatus);
        List<Shopping_cart> cartList = shoppingCartService.cartList(map);
        List<ShoppingCartVo> voList = new ArrayList<ShoppingCartVo>();
        for (Shopping_cart shoppingCart : cartList) {
            ShoppingCartVo vo = new ShoppingCartVo();
            vo.setUid(shoppingCart.getUid());
            vo.setProduct_count(shoppingCart.getProduct_count());
            vo.setProduct_status(shoppingCart.getProduct_status());
            vo.setId(shoppingCart.getId());
            vo.setProduct_id(shoppingCart.getProduct_id());
            Product product = productService.getById(shoppingCart.getProduct_id().intValue());
            vo.setProductName(product.getProduct_name());
            vo.setPrice(product.getPrice());
            voList.add(vo);
        }
        ModelAndView mav = new ModelAndView("shoppingCart/shoppingCartList");
        mav.addObject("voList", voList);
        return mav;
    }
    @RequestMapping("delete")
    public ModelAndView delete(Long uid,Long id){
        Map<String, Long> map = new HashMap<String, Long>();
        Long productStatus = 0L;
        map.put("productStatus", productStatus);
        map.put("uid", uid);
        map.put("id",id);
        Shopping_cart shopping_cart=shoppingCartService.getById(map);
        shopping_cart.setProduct_status(1L);
        int updateResult=shoppingCartService.update(shopping_cart);
        if (updateResult>0){
            return list(uid);
        }
        return null;
    }
}
